﻿using BLL;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using webui.Models;

namespace webui.Areas.Admin.Controllers
{
    public class CommentController : Controller
    {
        public NewsCommentBLL icomment = new NewsCommentBLL();



        /// <summary>
        /// 视图页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [SkipPemission]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [SkipPemission]
        public ActionResult Getlist()
        {
            //获取页容量
            int pageSize = 11;
            //获取请求的页码
            int pageIndex = 1;

            if (!string.IsNullOrEmpty(Request.Form["rows"]))
            {
                int.TryParse(Request.Form["rows"], out pageSize);
            }
            if (!string.IsNullOrEmpty(Request.Form["page"]))
            {
                int.TryParse(Request.Form["page"], out pageIndex);
            }
          
        
            PageModel page = new PageModel() {
                PageIndex = pageIndex,
                PageSize = pageSize
            }; 
            var list = icomment.GetPageList(p => p.id > 0, page, p => p.id, OrderByType.Desc);     // 查询分页数据
            //2 生成规定格式的 json字符串发回 给 异步对象
            DataGridModel dgModel = new DataGridModel()
            {
                total = page.PageCount,
                rows = list,
                footer = null
            };
            return Json(dgModel);
        }


        [SkipPemission]
        public ActionResult Sonindex()
        {
            int id = 0;
            int.TryParse(Request["nid"] ?? "0", out id);
            ViewBag.nid = id;
            return View();
        }


        [HttpPost]
        [SkipPemission]
        public ActionResult Getlistwhere()
        {
            int id = int.Parse(Request["nid"] ?? "0"); //新闻ID
            //获取页容量
            int pageSize = 11;
            //获取请求的页码
            int pageIndex = 1;

            if (!string.IsNullOrEmpty(Request.Form["rows"]))
            {
                int.TryParse(Request.Form["rows"], out pageSize);
            }
            if (!string.IsNullOrEmpty(Request.Form["page"]))
            {
                int.TryParse(Request.Form["page"], out pageIndex);
            } 
            PageModel page = new PageModel()
            {
                PageIndex = pageIndex,
                PageSize = pageSize
            };
            var list = icomment.GetPageList(p => p.newsid == id, page, p => p.id, OrderByType.Desc);
             
            //2 生成规定格式的 json字符串发回 给 异步对象
            DataGridModel dgModel = new DataGridModel()
            {
                total = page.PageCount,
                rows = list,
                footer = null
            };
            return Json(dgModel);
        } 

        public ActionResult Dlecomment(int id)
        {
            try
            {
                icomment.Delete(p => p.id == id);
                return OperateContext.Current.RedirectAjax("ok", "删除成功", null, "");
            }
            catch (Exception)
            {

                return OperateContext.Current.RedirectAjax("err", "操作失败", null, ""); ;
            }

        }


    }
}